
//  program:	Show permutation inference using simulated dataset created in r - Committee Partisanship
//  task:       *create replication file for most tables and models in submitted version of paper
				*some other tables are created using r files, see related files. Replication of permutation occurs in separatea stata file.
				*data developed in r
//				project: Committee Partisanship
// 				Related files: 08permutation_sim_060319 actually simulates the dataset. this just creates the graphs in the paper

//  author:     Josh Ryan \ June 5, 2019

// #Start
// program setup




use "simulatedata_m1_v1_123119.dta", clear

*generate variables where coefficietns and SEs will be stored
gen bmean=.
gen sd=.
gen p95=.
gen p5=.

describe

*summarize and save mean coefficients and and associated SEs
sum Estimate_lnke~t, de
replace bmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
gen var3="b_kernell" in 1
gen obsco=-.11 in 1

sum Estimate_lagp~o , de
replace bmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="b_lagpartydiff" in 2
replace obsco=.14 in 2

sum Estimt_lnkrnl~w, de
replace bmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="b_kernell_lagpartydiff" in 3
replace obsco=.15 in 3

keep bmean sd var3 obsco p95 p5

gen n=_n
drop if bmean==.



#delimit ;
twoway (scatter obsco n if n<4, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Coefficient Value", size()) xtitle("") ylabel(-.2(.05).2, valuelabel labsize())
  xlabel(1 `" "Logged Kernell" "District Ideology" "' 2 `" "Lagged Avg. Diff. in" "Comm. Part. Voting" "' 3 `" "Kernell Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Coefficient" 2 "Simulated 5-95% Coefficient Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Coefs.: Table 1, Model 1")
;
#delimit cr

graph save permutationcoef_kernell_1.gph, replace



use "simulatedata_m1_v1_123119.dta", clear

*generate variables where zs will be stored
gen zmean=.
gen sd=.
gen p95=.
gen p5=.
gen var3=""
gen obsco=.

describe

gen z_lnke_t=Estimate_lnkernelldispart/Std__Error_lnkernelldispart
sum z_lnke_t, de
replace zmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
replace var3="z_kernell" in 1
replace obsco=-2.75 in 1

gen z_lagp_f=Estimate_lagpartydiff/Std__Error_lagpartydiff
sum z_lagp_f, de
replace zmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="z_lagparty" in 2
replace obsco=2 in 2

rename Estimt_lnkrnl  Estimate_lnk_f
rename Std__Errr_lnkrnlldsprt Std_Errr_lnkrnlldsprt_f
gen lnkr_f=Estimate_lnk_f /Std_Errr_lnkrnlldsprt_f
sum lnkr_f, de
replace zmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="z_kernell_lagparty" in 3
replace obsco=2.14 in 3


keep zmean sd var3 obsco p95 p5

gen n=_n
drop if zmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Z-Value", size()) xtitle("") ylabel(-3(1)3, valuelabel labsize())
  xlabel(1 `" "Logged Kernell" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Kernell Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Z-Value" 2 "Simulated 5-95% Z-Value Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Z-Values: Table 1, Model 1")
;
#delimit cr

graph save permutationz_kernell_1.gph, replace


graph combine permutationcoef_kernell_1.gph permutationz_kernell_1.gph, scheme(s1mono) graphregion(fcolor(white) ifcolor(white))





****************************************************
*****Model 2-Dist. Ideology



*the other option is to bind the two together then draw a vertical line that divides them, then put text in the space on each side 
use "simulatedata_m2_v1_123119.dta", clear

*generate variables where coefficietns and SEs will be stored
gen bmean=.
gen sd=.
gen p95=.
gen p5=.

describe

*summarize and save mean coefficients and and associated SEs
sum Estimate_lndi~t, de
replace bmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
gen var3="b_distrct" in 1
gen obsco=-.1 in 1

sum Estimate_lagp~o, de
replace bmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="b_lagpartydiff_2" in 2
replace obsco=-.17 in 2

sum Estimate_lndi~w, de
replace bmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="b_district_lagpartydiff" in 3
replace obsco=.1 in 3

keep bmean sd var3 obsco p95 p5

gen n=_n
drop if bmean==.


#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Coefficient Value", size()) xtitle("") ylabel(-.2(.05).2, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Coefficient" 2 "Simulated 5-95% Coefficient Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Coefs: Table 1, Model 2")
;
#delimit cr

graph save permutationcoef_dispart_1.gph, replace


use "simulatedata_m2_v1_123119.dta", clear

*generate variables where zs will be stored
gen zmean=.
gen sd=.
gen p95=.
gen p5=.
gen var3=""
gen obsco=.

describe

sum z_value_lndis~t, de
replace zmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
replace var3="z_district" in 1
replace obsco=-1.43 in 1

sum z_value_lagpa~o, de
replace zmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="z_lagpartydiff" in 2
replace obsco=-.9 in 2

sum z_value_lndis~w, de
replace zmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="z_district_lagpartydiff" in 3
replace obsco=1.33 in 3


keep zmean sd var3 obsco p95 p5

gen n=_n
drop if zmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Z-Value", size()) xtitle("") ylabel(-3(1)3, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Z-Value" 2 "Simulated 5-95% Z-Value Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Z-Values: Table 1, Model 2")
;
#delimit cr

graph save permutationz_dispart_1.gph, replace


graph combine permutationcoef_dispart_1.gph permutationz_dispart_1.gph, scheme(s1mono) graphregion(fcolor(white) ifcolor(white))



*************************************************
****Model 3


*the other option is to bind the two together then draw a vertical line that divides them, then put text in the space on each side 
use "simulatedata_kernellnondistribute_v1_123119.dta", clear

*generate variables where coefficietns and SEs will be stored
gen bmean=.
gen sd=.
gen p95=.
gen p5=.

describe

*summarize and save mean coefficients and and associated SEs
sum Estimate_lnke~t, de
replace bmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
gen var3="b_kernell_approp" in 1
gen obsco=-.07 in 1

sum Estimate_lagp~f, de
replace bmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="b_lagpartydiff_approp" in 2
replace obsco=.27 in 2

sum Estimate_lnkr~f, de
replace bmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="b_kernell_lagpartydiff_approp" in 3
replace obsco=.15 in 3

keep bmean sd var3 obsco p95 p5

gen n=_n
drop if bmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Coefficient Value", size()) xtitle("") ylabel(-.3(.1).3, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Coefficient" 2 "Simulated 5-95% Coefficient Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Coefs: Table 2, Model 1")
;
#delimit cr

graph save permutationcoef_kernell_nonapprop.gph, replace


use "simulatedata_kernellnondistribute_v1_123119.dta", clear


*generate variables where zs will be stored
gen zmean=.
gen sd=.
gen p95=.
gen p5=.
gen var3=""
gen obsco=.

describe

sum z_value_lnker~t, de
replace zmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
replace var3="z_kernell_approp" in 1
replace obsco=-.78 in 1

sum z_value_lagpa~f, de
replace zmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="z_lagpartydiff" in 2
replace obsco=3.38 in 2


sum z_value_lnker~f, de
replace zmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="z_kernell_party" in 3
replace obsco=1.15 in 3


keep zmean sd var3 obsco p95 p5

gen n=_n
drop if zmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Z-Value", size()) xtitle("") ylabel(-3(1)3, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Z-Value" 2 "Simulated 5-95% Z-Value Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Z-Values: Table 2, Model 1")
;
#delimit cr

graph save permutationz_kernell_nonapprop.gph, replace


graph combine permutationcoef_kernell_nonapprop.gph permutationz_kernell_nonapprop.gph, scheme(s1mono) graphregion(fcolor(white) ifcolor(white))





*****************************************
*****Model 4




*the other option is to bind the two together then draw a vertical line that divides them, then put text in the space on each side 
use "simulatedata_kernelldistribute_v1_123119.dta", clear

*generate variables where coefficietns and SEs will be stored
gen bmean=.
gen sd=.
gen p95=.
gen p5=.

describe

*summarize and save mean coefficients and and associated SEs
sum Estimate_lnke~t, de
replace bmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
gen var3="b_kernell_approp" in 1
gen obsco=-.13 in 1

sum Estimate_lagp~f, de
replace bmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="b_lagpartydiff_approp" in 2
replace obsco=.05 in 2

sum Estimate_lnkr~f, de
replace bmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="b_kernell_lagpartydiff_approp" in 3
replace obsco=.09 in 3

keep bmean sd var3 obsco p95 p5

gen n=_n
drop if bmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Coefficient Value", size()) xtitle("") ylabel(-.3(.1).3, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Coefficient" 2 "Simulated 5-95% Coefficient Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Coefs: Table 2, Model 2")
;
#delimit cr

graph save permutationcoef_kernell_approp.gph, replace


use "simulatedata_kernelldistribute_v1_123119.dta", clear


*generate variables where zs will be stored
gen zmean=.
gen sd=.
gen p95=.
gen p5=.
gen var3=""
gen obsco=.

describe

gen z_lnke_t= Estimate_lnkernelldispart/Std__Error_lnkernelldispart
sum z_lnke_t, de
replace zmean=r(mean) in 1
replace sd=r(sd) in 1
replace p95=r(p95) in 1
replace p5=r(p5) in 1
replace var3="z_kernell_approp" in 1
replace obsco=-4.33 in 1

gen z_lagpar=Estimate_lagpartydiff/Std__Error_lagpartydiff
sum z_lagpar, de
replace zmean=r(mean) in 2
replace sd=r(sd) in 2
replace p95=r(p95) in 2
replace p5=r(p5) in 2
replace var3="z_lagpartydiff" in 2
replace obsco=-.71 in 2

rename Estimate_lnkrnlldsprt Estimate_lnkernellparty
rename Std__Errr_lnkrnlldsprt Std_Errr_lnkernellparty
gen z_kernell_partydiff=Estimate_lnkernellparty/Std_Errr_lnkernellparty
sum z_kernell_partydiff, de
replace zmean=r(mean) in 3
replace sd=r(sd) in 3
replace p95=r(p95) in 3
replace p5=r(p5) in 3
replace var3="z_kernell_party" in 3
replace obsco=1 in 3


keep zmean sd var3 obsco p95 p5

gen n=_n
drop if zmean==.

#delimit ;
twoway (scatter obsco n, lcolor(none) fcolor(gs14)) (rcap p95 p5 n, lcolor(black) lwidth(vthin) fcolor(none)), 
 ytitle("Z-Value", size()) xtitle("") ylabel(-2(1)5, valuelabel labsize())
  xlabel(1 `" "Logged Pres. Vote" "District Ideology" "' 2 `" "Avg. Diff. in Comm." "Partisan Voting" "' 3 `" "Pres. Ideo. x" "Diff. in Part. Voting." "',
  valuelabel labsize(small) angle(vertical))
 legend(col(1) order(1 "Observed Z-Value" 2 "Simulated 5-95% Z-Value Distribution")
 position(6) size(small)) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("Z-Values: Table 2, Model 2")
;
#delimit cr

graph save permutationz_kernell_approp.gph, replace


graph combine permutationcoef_kernell_approp.gph permutationz_kernell_approp.gph, scheme(s1mono) graphregion(fcolor(white) ifcolor(white))



